package com.ruoyi.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.common.util.ListConvertMap;
import com.common.util.MergeDataSource;
import com.param.impl.OrderAssignParam;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.order.domain.MgOrderAssign;
import com.ruoyi.order.service.IMgOrderAssignService;
import com.ruoyi.system.service.ISysUserService;

import io.swagger.annotations.ApiOperation;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 派稿Controller
 * 
 * @author ruoyi
 * @date 2024-05-19
 */
@RestController
@RequestMapping("/order/assign")
public class MgOrderAssignController extends BaseController
{
    @Autowired
    private IMgOrderAssignService mgOrderAssignService;
    
    private ISysUserService sysUserService;

    /**
     * 查询派稿列表
     */
    @GetMapping("/list")
    
    public TableDataInfo list(MgOrderAssign mgOrderAssign)
    {
        startPage();
        List<MgOrderAssign> list = mgOrderAssignService.selectList(mgOrderAssign);
        return getDataTable(list);
    }

    
    /**
     * 处理人列表
     */
    @ApiOperation(value = "处理人列表", tags = {"处理人列表"})
    @PostMapping("/list")
    public AjaxResult processedList(@RequestBody @Validated OrderAssignParam orderAssignParam)
    {

    	Long orderId = orderAssignParam.getOrderId();
    	
        List<MgOrderAssign> orderAssigns = mgOrderAssignService.getOrderAssigns(orderId);
        
        
        if (0 == orderAssigns.size()) {
        	return AjaxResult.success(orderAssigns);
        }
        
        long[] processeds = orderAssigns.stream().mapToLong(e->e.getProcessed()).distinct().toArray();
        
        List<SysUser> sysUsersByIds = sysUserService.getSysUsersByIds(processeds);
        
        MergeDataSource<MgOrderAssign> mergeDataSource = new MergeDataSource<MgOrderAssign>(orderAssigns);
        
        Map<Long, SysUser> listToMap = ListConvertMap.listToMap(sysUsersByIds, SysUser::getUserId);
        
        mergeDataSource.mergeRelease(MgOrderAssign::getProcessed, listToMap, (MgOrderAssign t, SysUser item) -> {
        	
        	t.setProcessName(item.getNickName());
        	
        	return t;
        });
        
        
		return success(orderAssigns);
    }

    
    
    /**
     * 导出派稿列表
     */
    @PreAuthorize("@ss.hasPermi('order:assign:export')")
    @Log(title = "派稿", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MgOrderAssign mgOrderAssign)
    {
        List<MgOrderAssign> list = mgOrderAssignService.selectList(mgOrderAssign);
        ExcelUtil<MgOrderAssign> util = new ExcelUtil<MgOrderAssign>(MgOrderAssign.class);
        util.exportExcel(response, list, "派稿数据");
    }

    /**
     * 获取派稿详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:assign:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(mgOrderAssignService.selectById(id));
    }

    /**
     * 新增派稿
     */
    @PreAuthorize("@ss.hasPermi('order:assign:add')")
    @Log(title = "派稿", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MgOrderAssign mgOrderAssign)
    {
        return toAjax(mgOrderAssignService.insert(mgOrderAssign));
    }

    /**
     * 修改派稿
     */
    @PreAuthorize("@ss.hasPermi('order:assign:edit')")
    @Log(title = "派稿", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MgOrderAssign mgOrderAssign)
    {
        return toAjax(mgOrderAssignService.update(mgOrderAssign));
    }

    /**
     * 删除派稿
     */
    @PreAuthorize("@ss.hasPermi('order:assign:remove')")
    @Log(title = "派稿", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(mgOrderAssignService.deleteByIds(ids));
    }

    @Resource
	public void setSysUserService(ISysUserService sysUserService) {
		this.sysUserService = sysUserService;
	}
}
